package com.example.function.integration.controller;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.util.DateUtils;
import com.example.function.integration.dto.DataReqDto;
import com.example.function.integration.service.CreditApplyDataExportService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;


/**
 * @Description: 华能授信申请数据导出
 * @ClassName: ImportAndExportController
 * @Author: ext.fengcheng
 * @Create: 2023-08-08 12:09
 */
@RestController
@RequestMapping("/huaneng")
public class CreditApplyDataExportController {

    @Resource
    private CreditApplyDataExportService creditApplyDataExportService;

    public static Logger logger = LoggerFactory.getLogger(CreditApplyDataExportController.class);

    /**
     * 数据导出参数说明：
     * {"tenantId":"morse_dev_demo","fileName":"华能授信数据文件-0","queryNum":100000,"sheetNum":1000000}
     * {
     *     "tenantId": "morse_dev_demo",          租户id
     *     "fileName": "华能授信数据文件-0",         导出的文件名称
     *     "queryNum": 100000,                    每次查询多少条数据，不传则默认为50万
     *     "sheetNum": 1000000                     每个sheet页存放多少条数据，不传则默认为100万
     *     "sheetPageNum": 5                       总共存放几个sheet页，不传则默认为5个
     * }
     */
    /**
     * @Description: 导出华能授信申请数据，可以同时导出多个sheet页的数据
     * @Author: ext.fengcheng
     * @Params: [request, response]
     * @Returns: java.lang.Boolean
     */
    @PostMapping("/creditApplyDataExport")
    public Boolean creditApplyDataExport(@RequestBody DataReqDto reqDto, HttpServletResponse response) {
        logger.info("creditApplyDataExport.导出多 Sheet 页实现，开始执行，开始时间：{}", DateUtils.toString(new Date()));
        if (ObjectUtils.isEmpty(reqDto)) {
            logger.error("参数校验为空！");
            return false;
        }
        String tenantId = reqDto.getTenantId();             // 租户id
        String queryNum = reqDto.getQueryNum();             // 导出的文件名称
        String fileName = reqDto.getFileName();             // 每次查询多少条数据，不传则默认为50万
        String sheetNum = reqDto.getSheetNum();             // 每个sheet页存放多少条数据，不传则默认为100万
        String sheetPageNum = reqDto.getSheetPageNum();     // 存放几个sheet页, 不传则默认为5个sheet页
        if (StringUtils.isAnyBlank(tenantId, fileName)) {
            logger.error("必填参数为空，reqDto：{}", JSONObject.toJSONString(reqDto));
            return false;
        }
        try {
            // 开始导出文件数据，处理过程中，发生异常则直接结束
            boolean result = creditApplyDataExportService.creditApplyDataExport(tenantId, Long.valueOf(queryNum), fileName, Long.valueOf(sheetNum), Integer.valueOf(sheetPageNum), response);
            if (!result) {
                logger.info("华能授信数据导出文件失败！");
                return false;
            }
            logger.info("华能授信数据导出文件成功，完成时间：{}", DateUtils.toString(new Date()));
            return true;
        } catch (Exception e) {
            logger.error("华能授信数据导出文件过程中发生异常，异常信息：{}", e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }
}
